// pages/searchfang/searchfang.js
import cache from '../../utils/Cache.js';
import url from '../../utils/url.js';
import http from '../../utils/http.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fangsList: [],
    page: 1,
    request: true,
    top_show: '',
    top: 0,
    attr:[],
    one_select: ['租期方式', '房源朝向', '租赁方式','租房小组'],
    show_hidden : ['hidden','hidden','hidden','hidden'],
	  jsonData:{type:'load',id:0,field:''},
    type:'aa'
  },    

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getFangList(this.data.jsonData);
    this.fangattr();
  },
  //获取房源列表
  getFangList: function (jsonData) {
    let that = this;
    //代表第一次加载
    if (jsonData.type == 'load') {
      that.data.type = 'aa';
    } else {
      that.data.type = 'ss'
    }
    //当没有数据的时候，在请求之后，发现没有就不允许调用数据了，但是一旦有了数据之后
    //只有刷新之后才能显示出来，不合理，所以为了减少服务器压力,隔一会就让再次可以执行
    setTimeout(() => {
      that.setData({
        request: true
      })
    }, 1000);

    //如果是false的话，就不进行数据请求
    if (!that.data.request) {
      wx.showToast({
        title: '已经没有更多的数据了',
        icon: 'none',
        duration: 1500,
        mask: true
      })
      return;
    }

    //在请求数据之前进行加载渲染
    wx.showLoading({
      title: '数据加载中',
      mask: true
    })
    //声明页码数
    let page = that.data.page;
	//搜索的字段名
	let fieldname = that.data.jsonData.field;
	//搜索的字段值
	let fieldvalue = that.data.jsonData.id;
    console.log(that.data.type + '--' + fieldname)

	if(fieldname && that.data.type == 'aa'){
		page = 1;
		that.data.fangsList = [];
	}
    http.http(url.fanglist, 'GET', { page,fieldname,fieldvalue}, function (res) {
      console.log(res.data)
      if (res.data.length > 0) {
        //页码进行加1
        page++;
        that.setData({
          //对两个数组进行组合
          fangsList: [...that.data.fangsList, ...res.data],
          page: page
        });
      } else {
        //如果没有数据了，就暂时不请求接口了
        if (fieldname && that.data.type == 'aa') {
          that.setData({
            fangsList:[],
            request: false
          })
        }else{
          that.setData({
            request: false
          })
        }
        
        wx.showToast({
          title: '已经没有更多数据了',
          icon: 'none',
          duration: 1500,
          mask: true
        })
      }

      //当请求到数据的时候，进行加载提示的关闭
      wx.hideLoading();
    });
  },
  //滚动时触发
  fangscroll: function (evt) {
    let that = this;
    if (evt.detail.scrollTop >= 300) {
      that.setData({
        top_show: 'top_show'
      })
    } else {
      that.setData({
        top_show: ''
      })
    }
  },
  //回到顶部
  reutnTop: function (evt) {
    let that = this;
    that.setData({
      top: 0
    })
  },
  //获取房源属性列表
  fangattr:function(){
    let that = this;
    http.http(url.fangattr,'GET',{},function(res){
      that.setData({
        attr:res
      });
    });
  },
  //点击一级菜单切换
  clickserach:function(evt){
      let that = this;
      let index = evt.currentTarget.dataset.index;
      that.data.show_hidden.map((item,index) => {
        that.data.show_hidden[index] = 'hidden';
      });
      that.data.show_hidden[index] = 'show';
      that.setData({
        show_hidden: that.data.show_hidden
      });
  },
  //点击二级菜单切换
  tapSubMenu:function(evt){
    let that = this;
    let index = evt.currentTarget.dataset.index;
    let name = evt.currentTarget.dataset.name;
  	let field = evt.currentTarget.dataset.attr;
	  let id = evt.currentTarget.dataset.id;
    that.data.one_select = ['租期方式', '房源朝向', '租赁方式', '租房小组'];
    that.data.one_select[index] = name;
    that.data.show_hidden.map((item, index) => {
      that.data.show_hidden[index] = 'hidden';
    });
    that.setData({
      one_select: that.data.one_select,
      show_hidden: that.data.show_hidden,
	    jsonData:{type:'load',id:id,field:field}
    })
    that.getFangList(that.data.jsonData);
  },
  //跳转到搜索页面
  search:function(evt){
	  wx.navigateTo({
		  url:'../../pages/search/search'
	  });
  }
 
})